<?

JCAT_Package::ImportClass('JCAT_ViewGtk',JCAT::PACKAGE_MVCGTK) ;

class UICtrlGtkTreeDlg extends JCAT_ViewGtk implements JCAT_IViewForm
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	array
	 */
	public function UICtrlGtkTreeDlg( JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_ViewGtk('UICtrlGtkTreeDlg','UICtrlGtkTree','UICtrlGtkTree',$aParent,$aUI) ;

		$aUICtrl = new LstColumns() ;
		$this->AddUICtrl($aUICtrl) ;

		// 设置数据交换
		$aDataExchanger = $this->GetDataExchanger() ;
		$aDataExchanger->LinkUICtrlToDocumentData('LstColumns','Columns') ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnNewColumnClicked()
	{
		$aLstColumns = $this->GetUICtrl('LstColumns') ;
		$aModel = $aLstColumns->GetGtkModel() ;
		
		// 插入 新行
		$aNewItem = $aModel->append(null,array('','Gtk::TYPE_STRING','Text',false,'',true,'-1')) ;

		// 定位到新行， 第一列 为 编辑状态
		$aLstColumns->EditCell($aNewItem,LstColumns::CLM_HEAD_NAME) ;		
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnRemoveColumnClicked()
	{		
		// 取得选中行
		list($aModel,$aItem) = $this->GetCurrentItem() ;
		if(!$aItem)
		{
			return ;
		}
		
		$aModel->remove($aItem) ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnUpColumnClicked()
	{
		$aLstColumns = $this->GetUICtrl('LstColumns') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aLstColumns,'JCAT_UIGtkCtrlTree') ;

		list($aModel,$aItem) = $this->GetCurrentItem() ;
		if(!$aItem)
		{
			return ;
		}
		
		$aTargetItem = $aLstColumns->GetPreviousItem($aItem) ;
		
		if(!$aTargetItem)
		{
			return ;
		}
		
		$aModel->swap($aItem,$aTargetItem) ;
	}
	
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnDownColumnClicked()
	{
		list($aModel,$aItem) = $this->GetCurrentItem() ;
		if(!$aItem)
		{
			return ;
		}
		
		// 目标 item
		$aTargetItem = $aModel->iter_next($aItem) ;
		
		if(!$aTargetItem)
		{
			return ;
		}
		
		$aModel->swap($aItem,$aTargetItem) ;
	}
	
	
	
	/**
	 * what's this
	 * 
	 * @access	protected
	 * @return	array
	 */
	protected function GetCurrentItem()
	{
		$aLstColumns = $this->GetUICtrl('LstColumns') ;
		$wndLstColumns = $aLstColumns->GetMainWidget() ;
		JCAT_ASSERT::ASSERT_INSTANCE($wndLstColumns,'GtkTreeView') ;
		
		$aSelection = $wndLstColumns->get_selection();
		list($aModel,$aItem) = $aSelection->get_selected() ;
		
		if(!$aItem)
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择项目')) ;
			return array($aModel,null) ; 
		}
		
		return array($aModel,$aItem) ; 
	}

}

?>